package pt.iscte.pcd.horserace;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Race {
	private static final int NUM = 200;
	public static final int MAX_STEPS = 30;
	private JFrame frame = new JFrame("Corrida");
	private JTextField[] tracks = new JTextField[NUM];
	private Horse[] horses = new Horse[NUM];

	public Race() {
		buildGUI();
		createHorses();
	}

	private void createHorses() {
		for (int i = 0; i < NUM; i++) {
			horses[i] = new Horse(tracks[i], horses);
		}
	}

	private void buildGUI() {
		Container c = frame.getContentPane();
		JPanel panel = new JPanel();
		for (int i = 0; i < NUM; i++) {
			tracks[i] = new JTextField(String.valueOf(MAX_STEPS));
			panel.add(tracks[i]);
		}
		c.add(panel);

		JButton start = new JButton("Start");
		c.add(start, BorderLayout.SOUTH);
		start.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				for (Horse horse : horses) {
					horse.start();
				}
			}
		});

		frame.setSize(200, 200);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	private void init() {
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		Race race = new Race();
		race.init();
	}

}
